package main

import (
	"fmt"
	"github.com/jinzhu/gorm"
	"gogogo/gorm/model"
	"time"
)
import 	_ "github.com/go-sql-driver/mysql"

/*
   Author: mahaiyuan
   Date: 2021/2/24 5:20 下午
   Description:
*/

func main() {
	db, err := gorm.Open("mysql", "root:12345678@tcp(localhost:3306)/gogogo?charset=utf8mb4&parseTime=True&loc=Local")
	db.LogMode(true)	//开启日志 打印执行的sql
	db.SingularTable(true)
	if err != nil {
		panic("fail to connect db")
	}
	defer db.Close()
	t := &model.Teacher{JobNumber: "20210020005", Birthday: time.Now(), WorkAge: 1}
	result := db.Create(t)
	// ----------------------------------
	//用指定的字段创建记录
	//INSERT INTO `teacher` (`job_number`,`birthday`) VALUES ('20210010001','2021-02-24 18:45:23')
	//result := db.Select("JobNumber", "Birthday").Create(t)

	// 批量插入
	/*teachers := []Teacher{
		{ID: 100,JobNumber: "20210020001", Birthday: time.Now(), WorkAge: 10},
		{ID: 101,JobNumber: "20210020002", Birthday: time.Now(), WorkAge: 9},
		{ID: 102,JobNumber: "20210020003", Birthday: time.Now(), WorkAge: 5}}
	result := db.Create(&teachers)*/
	fmt.Println("result =", result)
}

